<template>
  <div id="questionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题号" style="min-width: 240px">
        <a-input v-model="searchParams.questionId" placeholder="请输入" />
      </a-form-item>
      <a-form-item field="language" label="编程语言" style="min-width: 240px">
        <a-select
          v-model="searchParams.language"
          :style="{ width: '320px' }"
          placeholder="选择编程语言"
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>c</a-option>
          <a-option>go</a-option>
          <a-option>JavaScript</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
        <a-button
          style="margin: 0 5px 0 5px"
          type="primary"
          @click="
            () => {
              searchParams.language = undefined;
              searchParams.questionId = undefined;
            }
          "
          >重置
        </a-button>
        <a-button type="primary" @click="doSubmit">刷新</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <!--    {{ JSON.stringify(record.judgeInfo) }}  -->
      <!--   <template #judgeInfo="{ record }">
           {{
             `Message: ${
               record.judgeInfo.message !== null
                 ? judgeInfo[record.judgeInfo.message] ?? record.judgeInfo.message
                 : "判题中"
             } ~
             Memory: ${record.judgeInfo.memory}${
               record.judgeInfo.memory !== null ? " KB" : ""
             } ~
             Time: ${record.judgeInfo.time}${
               record.judgeInfo.time !== null ? " ms" : ""
             }`
           }}
         </template> -->
      <template #judgeInfo="{ record }">
        {{
          `Message: ${getJudgeInfo(record.judgeInfo.message)} ~
          Memory: ${record.judgeInfo.memory}${
            record.judgeInfo.memory !== null ? " KB" : ""
          } ~
          Time: ${record.judgeInfo.time}${
            record.judgeInfo.time !== null ? " ms" : ""
          }`
        }}
      </template>
      <template #judgeStatus="{ record }">
        {{ judgeStatus[record.status] }}
      </template>
      <template #userInfo="{ record }">
        {{ record.userVO.userAccount }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, toRaw, watchEffect } from "vue";
import {
  QuestionControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";
import { JudgeControllerService } from "../../../generated/services/JudgeControllerService";

const tableRef = ref();
// const judgeInfo = ref();
// const judgeStatus = ref();
let judgeInfo: any;
let judgeStatus: any;

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitQueryRequest>({
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});

// const judgeInfo = store.state.judgeInfo.judgeInfo.judgeResult;
// const judgeStatus = store.state.judgeInfo.judgeInfo.judgeStatus;

const router = useRouter();

const loadData = async () => {
  console.log("数据");
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
    if (res.code === 40100) {
      router.push({
        path: `/user/login`,
      });
    }
  }
};

const getJudgeInfo = (message: string | null) => {
  if (message == null) {
    return "判题中";
  }
  return judgeInfo[message];
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

let timer: any = null;

const loadJudgeInfo = async () => {
  const res = await JudgeControllerService.getJudgeInfoUsingGet();
  if (res.code === 0) {
    judgeInfo = toRaw(res.data?.judgeResult);
    judgeStatus = toRaw(res.data?.judgeStatus);
  }
};

/**
 * 页面加载时，请求数据
 */
onMounted(async () => {
  await loadJudgeInfo();
  await loadData();
  // judgeInfo = store.state.judgeInfo.judgeInfo;
  // 3 秒刷新一次
  timer = setInterval(loadData, 10000);
});

onBeforeUnmount(() => {
  clearInterval(timer);
});

const columns = [
  {
    title: "提交号",
    dataIndex: "id",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "判题状态",
    slotName: "judgeStatus",
  },
  {
    title: "题号",
    dataIndex: "questionId",
  },
  {
    title: "提交者账号",
    slotName: "userInfo",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
